package com.wuseguang.springcloud.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 时间工具
 *
 * @author zmc
 * @since 2019-12-10
 */
public class DateUtil {

    private static Logger logger = LoggerFactory.getLogger(DateUtil.class);

    /**
     * date转换String
     *
     * @param date   时间
     * @param format 格式 yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String parseData(Date date, String format) {
        //设置格式
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        //返回string的时间
        return sdf.format(date);
    }


    /**
     * @描述 获取时间的分钟
     * @创建人 zm
     * @创建时间 2020/7/8
     */
    public static String getMinuteTime(String time) {
        SimpleDateFormat format = null;
        Date parse = null;
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");//设置日期格式
            format = new SimpleDateFormat("HH:mm");
            parse = df.parse(time);
        } catch (ParseException e) {

        }
        return format.format(parse);
    }

    /**
     * @描述 获取当前时间
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    public static String time() {
        //设置格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        //返回string的时间
        return df.format(new Date());
    }

    /**
     * @描述 获取当前时间
     * @创建人 zm
     * @创建时间 2019/12/27
     */
    public static String getTimeAndMinutes() {
        //设置格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");//设置日期格式
        //返回string的时间
        return df.format(new Date());
    }

    /**
     * @描述 获取时间yyyy-MM-dd
     * @创建人 zm
     * @创建时间 2020/2/5
     */
    public static String gettime() {
        //设置格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        //返回string的时间
        return df.format(new Date());
    }

    public static String getdaytime(String time) {
        //设置格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String format = null;
        try {
            Date parse = df.parse(time);
            format = df.format(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //返回string的时间
        return format;
    }

    public static String getdaytime(String time, String pattern, String pattern2) {
        //设置格式
        SimpleDateFormat df = new SimpleDateFormat(pattern);//设置日期格式
        SimpleDateFormat df1 = new SimpleDateFormat(pattern2);//设置日期格式
        String format = null;
        try {
            Date parse = df.parse(time);
            format = df1.format(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //返回string的时间
        return format;
    }

    public static String AddTime(String day, String s) {
        String format = "";
        try {
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = f.parse(day);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            SimpleDateFormat ds = new SimpleDateFormat("yyyy-MM-dd ");
            Calendar c = Calendar.getInstance();
            c.setTime(parse);
            c.add(Calendar.DAY_OF_MONTH, 0);
            Date endTime = df.parse(ds.format(c.getTime()) + s);
            format = df.format(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return format;
    }

    private static String getWeek() {
        String week = "";
        Date today = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(today);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        if (weekday == 1) {
            week = "周日";
        } else if (weekday == 2) {
            week = "周一";
        } else if (weekday == 3) {
            week = "周二";
        } else if (weekday == 4) {
            week = "周三";
        } else if (weekday == 5) {
            week = "周四";
        } else if (weekday == 6) {
            week = "周五";
        } else if (weekday == 7) {
            week = "周六";
        }
        return week;
    }

    /**
     * 根据时间获取星期（中文）
     *
     * @param s
     * @return
     */
    public static String getWeeks(String s) {
        String week = "";
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            Calendar c = Calendar.getInstance();
            c.setTime(df.parse(s));
            int weekday = c.get(Calendar.DAY_OF_WEEK);
            if (weekday == 1) {
                week = "周日";
            } else if (weekday == 2) {
                week = "周一";
            } else if (weekday == 3) {
                week = "周二";
            } else if (weekday == 4) {
                week = "周三";
            } else if (weekday == 5) {
                week = "周四";
            } else if (weekday == 6) {
                week = "周五";
            } else if (weekday == 7) {
                week = "周六";
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return week;
    }

    /**
     * 根据时间获取星期（数字）
     *
     * @param s
     * @return
     */
    public static int getWeeksInt(String s) {
        Integer week = null;
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            Calendar c = Calendar.getInstance();
            c.setTime(df.parse(s));
            int weekday = c.get(Calendar.DAY_OF_WEEK);
            if (weekday == 1) {
                week = 6;
            } else if (weekday == 2) {
                week = 0;
            } else if (weekday == 3) {
                week = 1;
            } else if (weekday == 4) {
                week = 2;
            } else if (weekday == 5) {
                week = 3;
            } else if (weekday == 6) {
                week = 4;
            } else if (weekday == 7) {
                week = 5;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return week;
    }

    public static long getcompTime(String s1, String s2) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date time1 = null;
        Date time2 = null;
        long min = 0;
        try {
            time1 = format.parse(s1);
            time2 = format.parse(s2);
            logger.info("time1={},time2={}", time1, time2);
            long between = (time1.getTime() - time2.getTime()) / 1000;
            min = between / 60;
        } catch (ParseException e) {

        }
        return min;
    }

    /**
     * @描述 获取时间
     * @创建人 zm
     * @创建时间 2020/2/4
     */
    public static String getMTime(String s) {
        String format1 = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            Date parse = df.parse(s);
            format1 = format.format(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return format1;
    }

    /**
     * @描述 给 某个时间添加分钟
     * @创建人 zm
     * @创建时间 2020/1/2
     */
    public static String addtimeMinutes(String s, int minutes) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Calendar nowTime = null;
        try {
            Date parse = sdf.parse(s);
            nowTime = Calendar.getInstance();
            nowTime.setTime(parse);
            nowTime.add(Calendar.MINUTE, minutes);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return sdf.format(nowTime.getTime());
    }


    /**
     * @描述 求两个时间差
     * @创建人 zm
     * @创建时间 2020/2/4
     */
    public static long valueCompTime(String s1, String s2) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date time1 = null;
        Date time2 = null;
        long min = 0;
        try {
            time1 = format.parse(s1);
            time2 = format.parse(s2);
            logger.info("time1={},time2={}", time1, time2);
            long between = (time1.getTime() - time2.getTime()) / 1000;
            min = between / 60;
        } catch (ParseException e) {

        }
        return min;
    }


    /**
     * @描述 以下方法均为 开关机算法 需要用到。请勿改动
     * @创建人 zm
     * @创建时间 2020/1/2
     */


    /**
     * @描述 添加分钟
     * @创建人 zm
     * @创建时间 2020/1/2
     */
    public static String addMinutes(int minutes) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar nowTime = Calendar.getInstance();
        nowTime.add(Calendar.MINUTE, minutes);
        return sdf.format(nowTime.getTime());
    }

    /**
     * @描述 获取周几
     * @创建人 zm
     * @创建时间 2020/1/2
     */
    public static String getWeekEnglish(String s) {
        String week = "";
        try {
            SimpleDateFormat ds = new SimpleDateFormat("yyyy-MM-dd");
            Date time = ds.parse(s);
            Calendar c = Calendar.getInstance();
            c.setTime(time);
            int weekday = c.get(Calendar.DAY_OF_WEEK);
            if (weekday == 1) {
                week = "sunday";
            } else if (weekday == 2) {
                week = "monday";
            } else if (weekday == 3) {
                week = "tuesday";
            } else if (weekday == 4) {
                week = "wednesday";
            } else if (weekday == 5) {
                week = "thursday";
            } else if (weekday == 6) {
                week = "friday";
            } else if (weekday == 7) {
                week = "saturday";
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return week;
    }


    public static int getWeekInt(String s) {
        int weekday = 0;
        try {
            SimpleDateFormat ds = new SimpleDateFormat("yyyy-MM-dd");
            Date time = ds.parse(s);
            Calendar c = Calendar.getInstance();
            c.setTime(time);
            weekday = c.get(Calendar.DAY_OF_WEEK);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (weekday == 1) {
            weekday = 7;
        } else if (weekday == 2) {
            weekday = 1;
        } else if (weekday == 3) {
            weekday = 2;
        } else if (weekday == 4) {
            weekday = 3;
        } else if (weekday == 5) {
            weekday = 4;
        } else if (weekday == 6) {
            weekday = 5;
        } else if (weekday == 7) {
            weekday = 6;
        }
        return weekday;
    }

    public static String getAddTime(String day, String s) {
        String format = "";
        try {
            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = f.parse(day);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat ds = new SimpleDateFormat("yyyy-MM-dd ");
            Calendar c = Calendar.getInstance();
            c.setTime(parse);
            c.add(Calendar.DAY_OF_MONTH, 0);
            Date endTime = df.parse(ds.format(c.getTime()) + s);
            format = df.format(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return format;
    }

    /**
     * @描述 求两个时间的天数差
     * @创建人 zm
     * @创建时间 2020/1/3
     */
    public static long getDay(String startTime, String endTime) {
        long a = -1;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);
            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                a++;
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return a;
    }

    /**
     * @描述 增加天数
     * @创建人 zm
     * @创建时间 2020/2/4
     */
    public static String addDays(String startTime, int days) {
        String format = "";
        try {
            SimpleDateFormat ds = new SimpleDateFormat("yyyy-MM-dd");
            Date parse = ds.parse(startTime);
            Calendar c = Calendar.getInstance();
            c.setTime(parse);
            c.add(Calendar.DAY_OF_MONTH, days);
            Date endTime = ds.parse(ds.format(c.getTime()));
            format = ds.format(endTime);
        } catch (ParseException e) {
            format = "";
        }
        return format;
    }

    /**
     * 时间转换 String->date
     *
     * @param strDate 时间
     * @param pattern yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date strToDateLong(String strDate, String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static long compTime(String s1, String s2) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date time1 = null;
        Date time2 = null;
        long min = 0;
        try {
            time1 = format.parse(s1);
            time2 = format.parse(s2);
            long between = (time1.getTime() - time2.getTime()) / 1000;
            min = between / 60;
        } catch (ParseException e) {

        }
        return min;
    }

    public static String getNowTime() {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        return format.format(new Date());
    }

    public static int calcWeekOffset(String strBefore, String strAfter) {
        int weekOffset = -1;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date startTime = sdf.parse(strBefore);
            Date endTime = sdf.parse(strAfter);
            Calendar cal = Calendar.getInstance();
            cal.setTime(startTime);
            int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
            dayOfWeek = dayOfWeek - 1;
            if (dayOfWeek == 0) {
                dayOfWeek = 7;
            }
            int dayOffset = calcDayOffset(startTime, endTime);
            weekOffset = dayOffset / 7;
            int a;
            if (dayOffset > 0) {
                a = (dayOffset % 7 + dayOfWeek > 7) ? 1 : 0;
            } else {
                a = (dayOfWeek + dayOffset % 7 < 1) ? -1 : 0;
            }
            weekOffset = weekOffset + a;
            return weekOffset;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return weekOffset;
    }

    public static int calcDayOffset(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);
        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2) {  //同一年
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {  //闰年
                    timeDistance += 366;
                } else {  //不是闰年
                    timeDistance += 365;
                }
            }
            return timeDistance + (day2 - day1);
        } else { //不同年
            return day2 - day1;
        }
    }

    /**
     * 判断两个时间的大小
     *
     * @param time1   时间1
     * @param time2   时间2
     * @param pattern 时间格式（yyyy-MM-dd hh:mm:ss/yyyy-MM-dd）
     * @return
     * @throws ParseException
     */
    public static boolean compare(String time1, String time2, String pattern) {
        //如果想比较日期则写成"yyyy-MM-dd"就可以了
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        //将字符串形式的时间转化为Date类型的时间
        Date a = null;
        Date b = null;
        try {
            a = sdf.parse(time1);
            b = sdf.parse(time2);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //Date类的一个方法，如果a早于b返回true，否则返回false
        /*if (a.before(b))
            return true;
        else
            return false;*/
        //如果你不喜欢用上面这个太流氓的方法，也可以根据将Date转换成毫秒
        if (a.getTime() - b.getTime() <= 0)
            return true;
        else
            return false;
    }

    /**
     * 判断两个时间是否相同
     *
     * @param date1
     * @param date2
     * @return
     */
    private static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        boolean isSameYear = cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
        boolean isSameMonth = isSameYear
                && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
        boolean isSameDate = isSameMonth
                && cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);
        return isSameDate;
    }

    /**
     * date转String
     *
     * @param date
     * @return
     */
    public static String dateToString(Date date, String pattern) {

        //设置格式
        SimpleDateFormat df = new SimpleDateFormat(pattern);//设置日期格式
        //返回string的时间
        return df.format(date);
    }


    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }


    /**
     * 求两个时间的小时差
     *
     * @param startTime 过去时间
     * @param endTime   当前时间
     * @return
     */
    public static Map<String, Object> getHour(String startTime, String endTime) {

        Map<String, Object> map = new HashMap<>();
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date now = df.parse(endTime);//当前时间
            Date date = df.parse(startTime);//过去
            long l = now.getTime() - date.getTime();
            long day = l / (24 * 60 * 60 * 1000);
            long hour = (l / (60 * 60 * 1000) - day * 24);
            long min = ((l / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long s = (l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            System.err.println("" + day + "天" + hour + "小时" + min + "分" + s + "秒");
            map.put("day", day);
            map.put("hour", hour);
            map.put("min", min);
            map.put("s", s);


        } catch (Exception e) {
            e.printStackTrace();
            map.put("day", 0);
            map.put("hour", 0);
            map.put("min", 0);
            map.put("s", 0);
        }

        return map;
    }


    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds 精确到秒的字符串 毫秒
     * @param format
     * @return
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);

        return sdf.format(new Date(Long.valueOf(seconds)));
    }

    /**
     * 在当前时间增加分钟
     * @param minute 要增加的分钟数
     * @return
     */
    public static Date addMinutesTwo(Integer minute){
        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, minute);
        //返回Date类型
        return c.getTime();
    }

    /**
     * 在当前时间增加月份
     * @param month 要增加的分钟数
     * @return
     */
    public static Date addMonth(Integer month){



        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, month);
        //返回Date类型
        Date rDate = c.getTime() ;
        try {

            rDate =  new SimpleDateFormat("yyyy-MM-dd").parse(dateToString(c.getTime(),"yyyy-MM-dd HH:mm:ss"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return rDate;
    }

    /**
     * 在当前时间增加月份
     * @param month 要增加的分钟数
     * @return
     */
    public static Date addMonth(Date date ,Integer month){




        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, month);
        //返回Date类型
        Date rDate = c.getTime() ;
        try {

            rDate =  new SimpleDateFormat("yyyy-MM-dd").parse(dateToString(c.getTime(),"yyyy-MM-dd HH:mm:ss"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return rDate;
    }

    public static void main(String[] args) throws ParseException {
        Long time = (System.currentTimeMillis() + (Integer.valueOf("27268") * 1000) - 60000) / 1000;
        System.err.println(time);
    }
}
